package mo.offer_special;

import java.util.Arrays;
import java.util.List;

public class L035 {

    private int DAY_IN_MINUTES = 24 * 60;

    public int findMinDifference(List<String> timePoints) {
        String base = timePoints.get(0);
        int res = DAY_IN_MINUTES;
        int[] timeMinutes = new int[timePoints.size()];
        for (int i = 0; i < timePoints.size(); i++) {
            timeMinutes[i] = toMinutes(timePoints.get(i));
        }
        Arrays.sort(timeMinutes);
        for (int i = 0; i < timeMinutes.length - 1; i++) {
            res = Math.min(res, timeMinutes[i+1] - timeMinutes[i]);
        }
        int circle = DAY_IN_MINUTES - (timeMinutes[timeMinutes.length-1] - timeMinutes[0]);
        res = Math.min(res, circle);
        return res;
    }

    private int toMinutes(String time) {
        String[] hourAndMinutes = time.split(":");
        String hour = hourAndMinutes[0];
        String minute = hourAndMinutes[1];
        int convertHour = convertToNumber(hour);
        int convertMinute = convertToNumber(minute);
        return convertHour * 60 + convertMinute;
    }

    private int convertToNumber(String time) {
        int convertTime = 0;
        if (time.charAt(0) == '0') {
            convertTime = time.charAt(1) - '0';
        } else {
            convertTime = Integer.parseInt(time);
        }
        return convertTime;
    }

    public static void main(String[] args) {
        L035 test = new L035();
        System.out.println(test.findMinDifference(Arrays.asList("02:00","02:01","23:59")));
    }
}
